(function () {
    "use strict";

    var areRams = [false, false, false ,false],
        userInput,
        randomNumber = generateRandomFourDigitNumber(),
        rams = 0,
        sheep = 0,
        tries = 0;

    document.getElementById('check').addEventListener('click', function () {
        userInput = document.getElementById('input').innerText;
        tries++;

        rams = 0;
        sheep = 0;

        for (var i = 0; i < 4; i++) {
            areRams[i] = false;

            if (userInput[i] === randomNumber[i]) {
                rams++;
                areRams[i] = true;
            }
        }

        for (var i = 0; i < 4; i++) {
            if (areRams[i] === false) {
                for (var j = 0; j < 4; j++) {
                    if (randomNumber[i] === userInput[j]) {
                        sheep++;
                        break;
                    }
                }
            }
        }

        document.getElementById('ramsAndSheep').innerText = 'rams: ' + rams + '\n' + 'sheep: ' + sheep;
        if (rams === 4) {
            document.getElementById('ramsAndSheep').innerText = 'You won the game in ' + tries + ' tries!' + '\n' +
                'Enter your nickname in the blue field';
            document.getElementById('input').innerText = '';
            document.getElementById('check').innerText = 'Save';

            document.getElementById('check').addEventListener('click', function () {
                if (localStorage['highScores'] === undefined) {
                    localStorage['highScores'] = '';
                }

                localStorage['highScores'] += '\n' + document.getElementById('input').innerText + ':' + tries;
                var allScores = localStorage['highScores'];
                var lines = allScores.split('\n');
                var results = [];
                for (var z = 0; z < lines.length; z++) {
                    var line = lines[z].split(':');
                    var obj = {
                        nickname: line[0],
                        score: line[1]
                    };

                    results.push(obj);
                }

                results.sort(function (a, b) {
                    return a.score - b.score;
                });

                for (var i = 0, len = results.length; i < len; i++) {
                    if (results[i].score) {
                        document.getElementById('highScores').innerText +=
                            results[i].nickname + ': ' + results[i].score + '\n';
                    }
                }
            });
        }
    });



    function generateRandomFourDigitNumber() {
        var resultAsString = '';

        resultAsString += getRandomValue(1, 10);

        for (var i = 0; i < 3; i++) {
            resultAsString += getRandomValue(0, 10);
        }

        return resultAsString;
    }

    function getRandomValue(min,max)
    {
        return (Math.random() * (max-min) + min | 0);
    }
}());